package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.annotation.IsLoginAnno;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.CustomException;
import cn.wolfcode.p2p.base.query.RechargeOfflineQuery;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.business.domain.PlatformBankInfo;
import cn.wolfcode.p2p.business.domain.RechargeOffline;
import cn.wolfcode.p2p.business.domain.RechargeOnline;
import cn.wolfcode.p2p.business.domain.UserBankInfo;
import cn.wolfcode.p2p.business.service.IPlatformBankInfoService;
import cn.wolfcode.p2p.business.service.IRechargeOfflineService;
import cn.wolfcode.p2p.business.service.IRechargeOnlineService;
import cn.wolfcode.p2p.business.service.IUserBankInfoService;
import cn.wolfcode.p2p.hosting.service.IHostingService;
import cn.wolfcode.p2p.hosting.util.HostingUtil;
import cn.wolfcode.p2p.util.JSONResult;
import cn.wolfcode.p2p.util.PageResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RechargeController {

        @Autowired
        private IUserBankInfoService userBankInfoService;
        @Autowired
        private ILoginInfoService loginInfoService;
        @Autowired
        private IRechargeOfflineService rechargeOfflineService;
        @Autowired
        private IUserInfoService userInfoService;
        @Autowired
        private IPlatformBankInfoService platformBankInfoService;
        @Autowired
        private IRechargeOnlineService rechargeOnlineService;
        @Autowired
        private IHostingService hostingService;

        @RequestMapping("recharge")
        @IsLoginAnno
        public String recharge(Model model){
                //loginInfoService查出管理员的账户,再用ID去查出平台的银行卡
                List<PlatformBankInfo> banks  = platformBankInfoService.selectAll();
                model.addAttribute("banks",banks);
                return "recharge";
        }


        @RequestMapping("recharge_save")
        @ResponseBody
        @IsLoginAnno
        public JSONResult rechargeSave(RechargeOffline rechargeOffline){
                JSONResult jsonResult = new JSONResult();
                try {
                        rechargeOfflineService.saveRechargeOffline(rechargeOffline);
                    }catch (CustomException e ){
                        jsonResult.setMsg(e.getMessage());//自定义的异常,避免所有异常都返回同样的信息给客户
                    } catch (Exception e) {
                        e.printStackTrace();
                        jsonResult.setMsg("系统繁忙,请稍后再试");
                }
                 return jsonResult;
        }

        @RequestMapping("checkTradeCode")
        @ResponseBody
        public boolean checkTradeCode(String tradeCode){
                return !rechargeOfflineService.checkTradeCode(tradeCode);

        }

        /**
         * 用户查询充值记录的方法
         * @return
         */
        @RequestMapping("recharge_list")
        public String rechargeList(Model model, @ModelAttribute("qo")RechargeOfflineQuery qo){
                LoginInfo loginInfo = UserContext.getLoginInfo();
                qo.setApplierId(loginInfo.getId());
                PageResult pageResult = rechargeOfflineService.query(qo);
                model.addAttribute("pageResult",pageResult);
                return "recharge_list";
        }
        @RequestMapping("hostingRecharge")
        @ResponseBody
        public void hostingRecharge(BigDecimal amount, HttpServletResponse response){
                String html = rechargeOnlineService.apply(amount);
                try {
                        response.getWriter().print(html);
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }

        @RequestMapping("callBackRecharge")
        public String callBackRecharge(String tradeCode,Model model){
                //根据交易号找到充值记录渲染页面
                RechargeOnline re = rechargeOnlineService.selectByTradeCode(tradeCode);
                model.addAttribute("re",re);
                return "recharge_result";
        }
        @RequestMapping("callBackRechargeResult")
        @ResponseBody
        public String callBackRechargeResult(HttpServletRequest request){
                try{
                        Map<String, String> stringStringMap = HostingUtil.requestToMap(request);
                        HostingUtil.checkSign(stringStringMap);
                         return rechargeOnlineService.resultHandler(stringStringMap);
                }catch (CustomException e){
                        return "ERROR";
                }
        }
}
